package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    //分页查询菜品
    @Override
    public Page<Dish> findByPage(String name, Integer pageNum, Integer pageSize) {
        //1.根据菜品名称分页从菜品表查询列表
        //构造查询条件  name like
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        //StringUtils.isNotEmpty(name) 相当于name!=null && name !=" "
        wrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        //构造分页条件
        Page<Dish> page = new Page<>(pageNum, pageSize);
        //根据name分页查询菜品信息列表
        page = dishMapper.selectPage(page, wrapper);
        //2.遍历上面的列表,得到每一个菜品对象
        List<Dish> dishList = page.getRecords();
        if (CollectionUtil.isNotEmpty(dishList)){
        for (Dish dish : dishList) {
            //3.根据菜品category_id查询菜品分类
            Category category = categoryMapper.selectById(dish.getCategoryId());
            dish.setCategoryName(category.getName());
            //4.根据菜品id从口味表查询它的口味列表
            //SELECT * FROM `dish_flavor` WHERE dish_id = ''
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId,dish.getId());
            List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper1);
            dish.setFlavors(dishFlavorList);//封装菜品的口味
        }
        }


        return page;
    }
    //添加菜品
    //同一个业务中,发送多条增删改语句的时候,一定要考虑事务问题
    @Transactional
    @Override
    public void save(Dish dish) {
        //调用dish的基本信息保存在餐表里
        log.info("保存之前:"+dish.getId());
        dishMapper.insert(dish);
        log.info("保存之后:"+dish.getId());
        //获取dish的口味列表,遍历保存到口味表
        List<DishFlavor> dishFlavorList = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(dishFlavorList )){
            for (DishFlavor dishFlavor : dishFlavorList ) {
                dishFlavor.setDishId(dish.getId());
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }
    //根据主键查询
    @Override
    public Dish findById(Long id) {
        //查询菜品
        Dish dish = dishMapper.selectById(id);
        //查询菜品口味
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper);
        dish.setFlavors(dishFlavorList);//封装
        return dish;
    }
    //更新
    @Override
    public void update(Dish dish) {
        //1. 根据主键更新菜品表信息
        dishMapper.updateById(dish);

        //2. 根据菜品id删除口味表中相关口味
        //DELETE FROM dish_flavor WHERE dish_id = ?  条件删除
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());//WHERE dish_id = ?
        dishFlavorMapper.delete(wrapper);

        //3. 向口味表保存新的口味列表
        List<DishFlavor> dishFlavorList = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(dishFlavorList)) {
            for (DishFlavor dishFlavor : dishFlavorList) {
                dishFlavor.setDishId(dish.getId());
                //保存一个口味
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }
    //逻辑删除
    @Transactional//事务
    @Override
    public void deleteByIds(Long[] ids) {
        //要删除的商品是否全部停售
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId,ids);
        wrapper.eq(Dish::getStatus,1);
        Integer count = dishMapper.selectCount(wrapper);
        //select count(0) from dish where id in #{ids} and status=1;
        if (count>0){
            throw new CustomException("您要删除的商品还在在售状态,不能删除");
        }
        //删除
        for (Long id : ids) {
            //删除菜品口味
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId,id);
            dishFlavorMapper.delete(wrapper1);
            //删除菜品
            dishMapper.deleteById(id);
        }
    }
    //停售
    @Override
    public void status(Integer status, Long[] ids) {
        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            dishMapper.updateById(dish);
        }
    }
    //回显
    @Override
    public List<Dish> findList(Long categoryId,Integer status) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(categoryId !=null,Dish::getCategoryId,categoryId);
        wrapper.eq(Dish::getStatus,status);
        List<Dish> dishList = dishMapper.selectList(wrapper);
        //遍历集合
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId,dish.getId());
                List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavorList);
            }
        }
        return dishList;
    }
    @Override
    public List<Dish> findList(Long categoryId,String name,Integer status) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name !=null,Dish::getName,name);
        wrapper.eq(Dish::getStatus,status);
        List<Dish> dishList = dishMapper.selectList(wrapper);
        //遍历集合
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId,dish.getId());
                List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavorList);
            }
        }
        return dishList;
    }
}
